<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@page isELIgnored="false" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>新增客户交往记录</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link id="bootstrap-style" href="<c:url value="/css/bootstrap.min.css"/>" rel="stylesheet">
	<link href="<c:url value="/css/bootstrap-responsive.min.css"/>" rel="stylesheet">
	<link id="base-style" href="<c:url value="/css/style.css"/>" rel="stylesheet">
	<link id="base-style-responsive" href="<c:url value="/css/style-responsive.css"/>" rel="stylesheet">
	<link href="<c:url value="/css/index.css"/>" rel="stylesheet">
	<link rel="shortcut icon" href="<c:url value="/img/favicon.ico"/>">
	<link href="<c:url value="/css/page.css"/>" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="<c:url value="/css/jquery.datetimepicker.css"/>"/>
	<link href="<c:url value="/css/navuser.css"/>" rel="stylesheet">
	<link href="<c:url value="/css/but.css"/>" rel="stylesheet">
	<script src="<c:url value="/js/jquery-1.9.1.min.js"/>"></script>
	<script src="<c:url value="/js/jquery-migrate-1.0.0.min.js"/>"></script>
	<script type="text/javascript" src="<c:url value="/js/layer/layer.js"/>"></script>
	<script src="<c:url value="/js/laydate/laydate.js"/>"></script>
	<script src="<c:url value="/js/build/jquery.datetimepicker.full.js"/>"></script>
	<script src="<c:url value="/js/orgin.js"/>"></script>
	<script src="<c:url value="/js/filebut.js"/>"></script>
</head>
 <script type="text/javascript">
 $(function(){
	 $.ajax({  
         url: '<%=basePath%>crm/getCustomerNameNoBracket',
         type: 'GET',  
         async: false,  
         cache: false,
         contentType: false,  
         processData: false,  
         dataType: "json",
         success: function (result) {
       	 if(result.data.length == 0){
       		 alert("还未添加客户");
       		 $("#saveBtn").attr({"disabled":"disabled"});
       	 }else{
	       	 var custSelHtm = "";
	       	 $.each(result.data,function(index,val){
	         	custSelHtm += "<option value='"+val.id+"'>"+val.custName+"</option>";
	         });
	         $("#custSel").html(custSelHtm);
	       	 //加载联系人
	    	 loadLinkman();
	         }
       	 }
    });
	 laydate.render({
		  elem: '#cttDate'
		  ,type: 'datetime'
		  ,format: 'yyyy-MM-dd'
		});
	 $("#custSel").change(function(){
		 loadLinkman();
	 });
	 $("#linkmanSel").change(function(){
		 $("#lkmId").val($(this).val());
	 });
 });
 function loadLinkman(){
	 var custid = $("#custSel").find("option:selected").val();
	 $("#linkmanSel").html("");
	 $.ajax({  
         url: '<%=basePath%>crm/getLinkmanByCustId?custId='+custid,
         type: 'GET',  
         async: false,  
         cache: false,
         contentType: false,  
         processData: false,  
         dataType: "json",
         success: function (result) {
        	 if(result.data.length == 0){
				alert("该客户还未添加联系人");
				$("#saveBtn").attr({"disabled":"disabled"});
        	 }else{
	        	var linkmanSelHtm = "";
	        	$.each(result.data,function(index,val){
					if(index == 0){
						$("#lkmId").val(val.id);
					}
	        		linkmanSelHtm += "<option value='"+val.id+"'>"+val.lkmName+"</option>";
	        	});
	        	$("#linkmanSel").html(linkmanSelHtm);
	        	$("#saveBtn").removeAttr('disabled');
	        	$("#custId").val(custid);
        	 }
         }
    });
 }
 function closebutton() {
		var index = parent.layer.getFrameIndex(window.name);  
	 	parent.layer.close(index);  
	 }
 function save(){
     var formData = new FormData($("#contactform")[0]);
     $.ajax({  
          url: '<%=basePath%>crm/saveContact',
          type: 'POST',  
          data: formData,  
          async: false,  
          cache: false,
          contentType: false,  
          processData: false,  
          success: function (result) {
                if("addok"==result){
                    alert("添加记录成功");
                    closebutton();
                    window.parent.location.reload(); //刷新父页面
                }else if("addno"==result){
                    alert("添加记录失败");
                }else if("custIsBracket"==result){
                	alert("该客户已封档");
                }
          }
     });
 }
 
		 </script>
		<body>
	
			<div id="content" class="span10" style="width: 100%;background-color:#F9F9F9;">
				
				<div style="background-color:#FFF;height: 710px">
				<div style="margin-top:-10px;margin-top:-10px;margin-bottom:20px;margin-left:20px;margin-right:20px;padding-top:20px;">
					<span style="color:#37c2c2">新增客户交往记录</span>
				</div>
				<form name="contactform" id="contactform" method="post">
					<input type="hidden" id="custId" name="custId" value="">
					<input type="hidden" id="lkmId" name="lkmId" value="">
					<div class="row-fluid content-table">
						<table class="table table-striped table-bordered bootstrap-datatable">
							<tbody>
								<tr>
									<td>公司</td>
									<td><select style="margin-left: -152px;" id="custSel"></select></td>
								</tr>
								<tr>
									<td>联系人</td>
									<td><select style="margin-left: -152px;" id="linkmanSel"></select></td>
								</tr>
								<tr>
									<td>时间</td>
									<td><input style="margin-left: -152px;" type="text" id="cttDate" name="cttDate" value=""></td>
								</tr>
								<tr>
									<td>概要</td>
									<td><input style="margin-left: -152px;" type="text" id="cttTitle" name="cttTitle" value=""></td>
								</tr>
								<tr>
									<td>地点</td>
									<td><input style="width: 360px;" type="text" id="cttPlace" name="cttPlace" value=""></td>
								</tr>
								<tr>
									<td>描述</td>
									<td>
									<textarea style="width: 360px;" rows="6" cols="" id="cttDesc" name="cttDesc"></textarea>
								</tr>
							</tbody>
						</table>
					</div>		
				</form>
			<div class="row-fluid content-table">
					<table class="table  bootstrap-datatable" style=" border:none; border-spacing:0; border-collapse:collapse;margin:0;padding:0;">
					  <tbody>
						<tr class="content-search" style="text-align: center;">
							<td colspan="6">
								<div>
									<button style="background-color: #dadada;color: #666;" type="button" onclick="closebutton();" id="output1">取消</button>
									<button id="saveBtn" class="but" onclick="save();">保存</button>
								</div>
							</td>
						</tr>
					  </tbody>
				  </table>            
				</div>
			</div>
		</div>	
</body>
</html>
